package com.example.administrator.activity.Homework.music;

import android.content.ComponentName;
import android.content.Intent;
import android.content.ServiceConnection;
import android.os.IBinder;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.example.administrator.activity.R;

public class MusicActivity extends AppCompatActivity implements View.OnClickListener {
private EditText path;
private Intent intent;
private myConn myConn;
MusicService.MyBinder binder;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_music);

        path=findViewById(R.id.music_ed_inputpath);
        findViewById(R.id.music_tv_play).setOnClickListener(this);
        findViewById(R.id.music_tv_pause).setOnClickListener(this);
        myConn=new myConn();
        intent=new Intent(this,MusicService.class);
        bindService(intent,myConn,BIND_AUTO_CREATE);
    }

    private class myConn implements ServiceConnection{

        @Override
        public void onServiceConnected(ComponentName name, IBinder service) {
            binder= (MusicService.MyBinder) service;
        }

        @Override
        public void onServiceDisconnected(ComponentName name) {

        }
    }

    @Override
    public void onClick(View v) {
        String pathway=path.getText().toString().trim();
        switch (v.getId()){
            case R.id.music_tv_play:
                if (!TextUtils.isEmpty(pathway)){
                    binder.play(pathway);
                    Toast.makeText(this, "开始播放", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "找不到音乐文件", Toast.LENGTH_SHORT).show();
                }
                break;
            case R.id.music_tv_pause:
                binder.pause();
                Toast.makeText(this, "停止播放", Toast.LENGTH_SHORT).show();
                break;
                default:
                    break;
        }
    }

    @Override
    protected void onDestroy() {
        unbindService(myConn);
        super.onDestroy();
    }
}
